package com.sitc.street_lab_2013.entityLogic;

import java.util.ArrayList;
import java.util.List;

import com.sitc.street_lab_2013.helpers.*;

import com.sitc.street_lab_2013.entities.Answer;
import com.sitc.street_lab_2013.entities.Location;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class LocationLogic {

	private SQLiteDatabase database;
	private DBHelper dbHelper;
	private String [] columns = { 
			DBHelper.LOC_ID,
			DBHelper.LOC_QUESTION,
			DBHelper.LOC_NAME,
			DBHelper.LOC_IMAGE,
			DBHelper.LOC_LAT,
			DBHelper.LOC_LONG,
			DBHelper.LOC_ISANSWERED};
	
	public LocationLogic(Context c){
		dbHelper = new DBHelper(c);
	}
	
	public void open() throws SQLException {
		database = dbHelper.getWritableDatabase();
	}
	
	public void close() {
		dbHelper.close();
	}
	
	public boolean createLocation(Location loc)
	{
		try {
			open();
			
			ContentValues values = new ContentValues();
			values.put(DBHelper.LOC_QUESTION, loc.getLocationQuestion());
			values.put(DBHelper.LOC_NAME, loc.getLocationName());
			values.put(DBHelper.LOC_IMAGE, loc.getLocationImage());
			values.put(DBHelper.LOC_LAT, loc.getLocationLat());
			values.put(DBHelper.LOC_LONG, loc.getLocationLong());
			values.put(DBHelper.LOC_ISANSWERED, loc.getLocationIsAnswered());
			
			long insertId = database.insert(DBHelper.TABLE_LOCATIONS, null, values);
			
			if (insertId != -1)
				return true;
		}
		catch(Exception e){
			Log.e("ERROR CREATE LOCATION, ", e.getCause().getMessage());
		} finally {
			close();
		}
		
		return false;
	}
	

	public int deleteAllLocations() {
		try {
			open();
			return database.delete(DBHelper.TABLE_LOCATIONS, null, null);

		} catch (Exception e) {
			Log.e("ERROR DELETE ALL LOCATION", e.getCause().getMessage());
		} finally {
			close();
		}

		return 0;
	}
			
	
	public ArrayList<Location> getLocationById(int locId)
	{
		ArrayList<Location> locations = new ArrayList<Location>();
		Cursor c = null;
		
		try{
			open();
			
			c = database.rawQuery(
				"SELECT * " 
				+ " FROM " + DBHelper.TABLE_LOCATIONS
				+ " WHERE " + DBHelper.LOC_ID + " = " + locId + ";", null);
			
			c.moveToFirst();
			
			while(!c.isAfterLast()){
				Location loc= cursorToLocation(c);
				locations.add(loc);
				c.moveToNext();
			}
		}
		catch(Exception e){
			Log.e("ERROR GET LOCATION", e.getCause().getMessage());
		} finally {
			close();
		}
		
		return locations;
	}
	
	public void updateLocation(int id)
	{
		try
		{
			open();
			
			Cursor cu = database.rawQuery(
				"UPDATE " + DBHelper.TABLE_LOCATIONS +
				" SET " + DBHelper.LOC_ISANSWERED + " = 1" +
				" WHERE " + DBHelper.LOC_ID + " = " + id + ";", null);

			cu.moveToFirst();
			cu.close();
		}
		catch(Exception e)
		{
			Log.e("ERROR UPDATE LOCATION", e.getCause().getMessage());
		}
		finally{
			close();
		}
	}

	public List<Location> getAllLocations() {
		ArrayList<Location> locations = null;

		try {

			open();
			locations = new ArrayList<Location>();
			Cursor c = database.query(DBHelper.TABLE_LOCATIONS, columns, null,
					null, null, null, null);

			// rawquery is used to easily integrate aggregate functions
			// database.rawQuery("SELECT id, date(created_at), content, retweet_count FROM tweets ORDER BY date(created_at) ASC",
			// null);
			
			c.moveToFirst();

			while (!c.isAfterLast()) {
				Location loc = cursorToLocation(c);
				locations.add(loc);
				c.moveToNext();
			}
		} catch (Exception e) {
			Log.e("ERROR GET ALL LOCATIONS", e.getCause().getMessage());
		} finally {
			close();
		}
		return locations;
	}
	
	public Location cursorToLocation(Cursor c){		
		Location l = new Location(
					c.getInt(0),
					c.getString(1), 
					c.getString(2), 
					c.getString(3), 
					c.getDouble(4), 
					c.getDouble(5),
					c.getInt(6));
		
		return l;
	}
}
